﻿using System;
using System.IO;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DreamCube.Foundation.Basic.Utility;
using DreamCube.Framework.DataAccess.Basic;
using DreamCube.Framework.DataAccess.Sqlserver;

namespace DreamCube.Websites.BLL
{
    /// <summary>
    /// 系统类
    /// </summary>
    public class WebSystem
    {
        #region "静态字段"
        
        /// <summary>
        /// system节点的XPath值
        /// </summary>
        private static String systemPath = "/SystemList/System";

        /// <summary>
        /// system节点的XPath值（帅选ID的值）
        /// </summary>
        //private static String systemByIDPath = "/SystemList/System[@id='{0}']";

        /// <summary>
        /// 数据库配置文件路径
        /// </summary>
        private static String databaseConfigFilePath  = @"Database\Config.xml";

        #endregion

        #region "私有字段"

        private String currentDatabaseConfigFilePath  = @"Database\Config.xml";

        /// <summary>
        /// 连接字符串集合
        /// </summary>
        private ConnectionStrings connectionStrings = null;

        /// <summary>
        /// 当前日志的保存目录
        /// </summary>
        private String logPath = String.Empty;

        /// <summary>
        /// 系统当前的首页Url地址
        /// </summary>
        private String indexPage ="Common/Pages/Index.aspx";

        #endregion

        #region "属性"

        /// <summary>
        /// 数据库配置文件路径
        /// </summary>
        public String DatabaseConfigFilePath
        {
            get
            {
                if(String.IsNullOrEmpty(currentDatabaseConfigFilePath))
                    return WebSystem.databaseConfigFilePath;
                else return currentDatabaseConfigFilePath;
            }
            set{
                this.currentDatabaseConfigFilePath = value;
            }
        }

        /// <summary>
        /// 当前系统的连接字符串
        /// </summary>
        public ConnectionStrings ConnectionStrings
        {
            get { return this.connectionStrings; }
        }

        /// <summary>
        /// 系统的ID，长度不受限制，原则上只要不与其它System的ID相同即可，如：id="S21"
        /// </summary>
        public String ID
        {
            get;
            set;
        }

        /// <summary>
        /// 系统的标题，如：Name="贵阳市测绘院测绘信息管理系统"
        /// </summary>
        public String Name
        {
            get;
            set;
        }

        /// <summary>
        /// 该项目的项目目录的路径，如：Path="Projects/Films"
        /// </summary>
        public String Path
        {
            get;
            set;
        }

        /// <summary>
        /// 指定系统的主界面用哪个页面，不设置该属性的话，系统默认的主界面是：Projects\Index.aspx
        /// 如：IndexPage='Projects/Films/Index.aspx'
        /// </summary>
        public String IndexPage
        {
            get { return this.indexPage; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    this.indexPage = value;
            }
        }

        /// <summary>
        /// 当前系统的日志目录
        /// </summary>
        public String LogPath
        {
            get;
            set;
        }

        #endregion

        #region "实例方法"

        /// <summary>
        /// 根据配置的连接字符串序号，创建数据库实例对象
        /// </summary>
        /// <param name="dbConnectionStringIndex"></param>
        /// <returns></returns>
        public Database GetDB(Int32 dbConnectionStringIndex = 0)
        {
            return DBManager.GetDBByConnectionString(this.ConnectionStrings[dbConnectionStringIndex]);
        }

        /// <summary>
        /// 根据配置的连接字符串配置的name属性值，创建数据库实例对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Database GetDB(String name)
        {
            return DBManager.GetDBByConnectionString(this.ConnectionStrings[name]);
        }

        #endregion

        #region "静态方法"

        /// <summary>
        /// 根据配置的数据库连接字符串的序号，获取数据库实例
        /// </summary>
        /// <param name="connectionStringIndex"></param>
        /// <returns></returns>
        public static Database GetCurrentSystemDB(Int32 dbConnectionStringIndex = 0)
        {

            return DBManager.GetDBByConnectionString(WebSession.CurrentSystem.ConnectionStrings[dbConnectionStringIndex]);
        }

        /// <summary>
        /// 获取所有系统
        /// </summary>
        /// <param name="configFilePath">xml配置文件路径</param>
        /// <returns></returns>
        public static List<WebSystem> GetAllSystems(String configFilePath)
        {
            XmlDocument doc = MyXml.CreateDocByFilePath(configFilePath);
            XmlNodeList systemNodes = doc.SelectNodes(systemPath);
            List<WebSystem> webSystemList = new List<WebSystem>();
            if (systemNodes != null && systemNodes.Count > 0)
            {
                for (Int32 k = 0; k < systemNodes.Count; k++)
                {
                    WebSystem webSystem = new WebSystem();
                    MyObject.LoadPropertyValueFromXmlNode(systemNodes[k], webSystem);
                    //读取当前系统配置的连接字符串
                    String dbConfigFilePath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), webSystem.Path + "\\" + webSystem.DatabaseConfigFilePath);
                    //获取系统所有配置的数据库连接字符串
                    webSystem.connectionStrings = ConnectionStrMgr.CreateConnectionStrings(dbConfigFilePath);
                    webSystemList.Add(webSystem);
                }
            }
            return webSystemList;
        }

        /// <summary>
        /// 根据系统的XML配置文件路径和系统的ID获取WebSystem对象
        /// </summary>
        /// <param name="configFilePath">xml配置文件路径</param>
        /// <param name="systemID">系统ID号</param>
        /// <returns></returns>
        public static WebSystem GetSystem(String configFilePath, String systemID)
        {
            List<WebSystem> systemList = GetAllSystems(configFilePath);
            for (Int32 i = 0; i < systemList.Count; i++)
            {
                if (MyString.CompareEx(systemList[i].ID, systemID) == 0)
                    return systemList[i];
            }
            return null;
        }

        /// <summary>
        /// 根据系统的XML配置文件路径获取配置中的第一个系统对象
        /// </summary>
        /// <param name="configFilePath"></param>
        /// <returns></returns>
        public static WebSystem GetFirstSystem(String configFilePath)
        {
            List<WebSystem> systemList = GetAllSystems(configFilePath);
            if (systemList != null && systemList.Count > 0) return systemList[0];
            return null;
        }

        #endregion
    }
}
